<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="module">
      run(async function () {
        const {
          React: { createElement },
          ReactDOM: { render },
          WebChat: { ReactWebChat }
        } = window; // Imports in UMD fashion.

        const { directLine, store } = testHelpers.createDirectLineEmulator();

        const styleOptions = {
          botAvatarBackgroundColor: 'red',
          botAvatarInitials: 'B',
          userAvatarBackgroundColor: 'blue',
          userAvatarInitials: 'TJ'
        };

        render(createElement(ReactWebChat, { directLine, store, styleOptions }), document.getElementById('webchat'));

        await pageConditions.uiConnected();

        // WHEN: Send a message.
        await (
          await directLine.actPostActivity(() =>
            pageObjects.sendMessageViaSendBox('echo "Hello, World!"', { waitForSend: false })
          )
        ).resolveAll();

        // WHEN: Received 2 messages.
        await directLine.emulateIncomingActivity({
          type: 'message',
          id: crypto.randomUUID(),
          from: { id: 'webchat-mockbot', name: 'webchat-mockbot', role: 'bot' },
          text: 'Echoing back in a separate activity.'
        });

        await directLine.emulateIncomingActivity({
          type: 'message',
          id: crypto.randomUUID(),
          from: { id: 'webchat-mockbot', name: 'webchat-mockbot', role: 'bot' },
          text: 'Hello, World!'
        });

        // THEN: Should show 3 messages.
        await pageConditions.numActivitiesShown(3);

        // THEN: Should match snapshot.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
